//首页底部选项卡
import React, { Component } from 'react'
import Home from '../../components/home'
import Postion from '../../components/postion'
import Trends from '../../components/trends'
import Me from '../../components/me'
import System from '../../components/system/systemIndex'
import ScrollableTabView from 'react-native-scrollable-tab-view';
import TabBar from '../../util/TabBar'
import { NavigationActions } from 'react-navigation';
import stirageUtil from '../../util/AsyncStorageUtil'
import Loading from '../../util/loading/loading'
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { loginForToken } from '../../actions/userAction'

//企业
import Home_c from '../../components/home_c/home/home_c'
import Managepos2 from '../../components/managepos_c/havepos/manage_pos2'
import Manageres_c from '../../components/manageres_c/manageres_c'
import Csystem_all from '../../components/system_c/csystem_all'
import Newsall_c from '../../components/home_c/news_c/newsall_c'

import {
    Text,
    Animated
} from 'react-native';
// '示例' 'ios-person-add'
var AnimatedValues = [new Animated.Value(0.9),new Animated.Value(0.9),new Animated.Value(0.9),new Animated.Value(0.9),new Animated.Value(0.9)];
var tabNames = ['首页', '职位', '动态', '我的', '系统'];
var tabIconNames = ['ios-home-outline', 'ios-calendar-outline', 'ios-nuclear-outline', 'ios-contact-outline', 'ios-settings-outline'];
var tabNames2 = ['首页', '职位', '简历','消息', '系统'];
var tabIconNames2 = ['ios-home-outline', 'ios-paper-plane-outline', 'ios-list-box-outline', 'ios-filing-outline', 'ios-settings-outline'];
const isFirst = true;

// var tabNames2 = ['首页']
// var tabIconNames2 = ['ios-home-outline']


class App extends Component {

    constructor(props) {
        super(props)
        this.state = {
            isloading: true,
            userType:0
        }
    }

    componentWillMount() {
        //判断是否登录
        const toKen = stirageUtil.get('accountToken').then((toKen) => {
            if (!toKen) {
                this.passing()
            } else {
                this.props.actions.loginForToken({ loginToken: toKen })
            }
        });
    }

    componentDidUpdate() {
        if (this.state.isloading) {
            if (this.props.tokenData.data.msg == "ok") {
                stirageUtil.get('userType').then((userType) => {
                    this.setState({ 
                        isloading: false,
                        userType:userType
                    })
                });
            } else {
                this.passing();
            }
        }
    }

    passing() {
        resetAction = NavigationActions.reset({
            index: 0,
            actions: [
                NavigationActions.navigate({ routeName: 'Login' })//要跳转到的页面名字
            ]
        });
        this.props.navigation.dispatch(resetAction);
    }


    render() {
        if (this.state.isloading) {
            return (
                <Loading />
            )
        } else if(this.state.userType==0){
            return (
                <ScrollableTabView
                    style={{flex: 1,backgroundColor:'#ffffff',borderTopWidth:0.5}}
                    locked={true}
                    renderTabBar={() => <TabBar tabNames={tabNames} tabIconNames={tabIconNames} AnimatedValues={AnimatedValues}/>}
                    scrollWithoutAnimation={true}//关闭动画
                    tabBarPosition='bottom'>
                    <Home tabLabel='首页' navigation={this.props.navigation}/>
                    <Postion tabLabel='职位' navigation={this.props.navigation}/>
                    <Trends tabLabel='动态' navigation={this.props.navigation}/>
                    <Me tabLabel='我的' navigation={this.props.navigation}/>
                    <System tabLabel='系统' navigation={this.props.navigation}/>
                    {/* <Test tabLabel='示例' navigation={this.props.navigation}/> */}
                </ScrollableTabView>
            )
        }else if(this.state.userType==1){
            return(
                <ScrollableTabView
                    style={{ flex: 1 ,backgroundColor:'#ffffff',borderTopWidth:0.5}}
                    locked={true}
                    renderTabBar={() => <TabBar tabNames={tabNames2} tabIconNames={tabIconNames2} AnimatedValues={AnimatedValues}/>}
                    scrollWithoutAnimation={true}//关闭动画
                    tabBarPosition='bottom'>
                    <Home_c tabLabel='首页' navigation={this.props.navigation} />
                    <Managepos2 tabLabel='职位' navigation={this.props.navigation} />
                    <Manageres_c tabLabel='简历' navigation={this.props.navigation} />
                    <Newsall_c tabLabel='消息' navigation={this.props.navigation} />
                    <Csystem_all tabLabel='系统' navigation={this.props.navigation} />
                    {/* <Test tabLabel='示例' navigation={this.props.navigation}/> */}
                </ScrollableTabView>
            )
        }else{
            return <Text>....</Text>
        }
    }
}

//组合action
var actions = {
    loginForToken
}
//连接state到props
const mapStateToProps = state => {
    return {
        tokenData: state.loginTokenReducer
    }
}
//连接action
const mapDispatchToProps = (dispatch) => {
    return {
        actions: bindActionCreators(actions, dispatch)
    }
}
//导出组合好的组件(组合后状态改变重新渲染组合组件的页面)
export default connect(
    mapStateToProps, mapDispatchToProps
)(App);
